#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QPropertyAnimation>
#include <QPoint>
#include <QPixmap>
#include <QTimer>
#include <QPainterPath>
#include <QPen>
#include <QKeyEvent>
#include <QFont>
#include <QBrush>
#include <QColor>
#include <QPainter>
#include <QRadioButton>
#include <QButtonGroup>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QTimer>
#include "commSignal.h"
#include "commWithBridge.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class AnimationWidget : public QWidget {
    Q_OBJECT

public:
    AnimationWidget(QWidget *parent = nullptr, class MainWindow * mainWindow = nullptr);
    ~AnimationWidget() override;
    void processStep1();
    void processStep2();
    void processStep3();
    void processStep4();
    void processStep5();
    void processStep6();

protected:
    void paintEvent(QPaintEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void updatePositions();

private:
    class MainWindow * mainWindow;

    QPoint robotPos;
    QPoint truckPos;
    QPoint trashPos1;
    QPoint trashPos2;
    QPoint trashPos3;

    QPixmap robotPixmap;
    QPixmap robotWorkPixmap;
    QPixmap truckPixmap;
    QPixmap trashPixmap;

    QRect chargingRoomRect;
    int chargingRoomRectWidth = 120;
    int chargingRoomRectHeight = 120;
    int chargingRoomRectMargin = 80;

    QTimer *updateTimer;

    QPainterPath chargingRoomToTrashPath;
    QPoint chargingToTrashPathCtrlP;
    QPoint chargingToTrashPathStartP;
    QPoint chargingToTrashPathEndP;

    QPainterPath trashToTruckPath;
    QPoint trashToTruckPathCtrlP;
    QPoint trashToTruckPathStartP;
    QPoint trashToTruckPathEndP;

    QPainterPath truckToTrashPath;
    QPoint truckToTrashPathCtrlP;
    QPoint truckToTrashPathStartP;
    QPoint truckToTrashPathEndP;

    QPainterPath trashToChargingPath;
    QPoint trashToChargingPathCtrlP;
    QPoint trashToChargingPathStartP;
    QPoint trashToChargingPathEndP;

    qreal t1;
    qreal t2;
    qreal t3;
    qreal t4;

    QPoint startPoint;
    QPoint middlePoint;
    QPoint targetPoint;

    bool passedMiddlePoint;
    bool robotReachedTruck;
    // bool carMovingToChargingRoom;

    int currentStage;

    void updatePosition(const QPainterPath& path, double& t, double targetT, std::function<void()> callback);
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void setLabelStepGrey(int stepNum);
    void setLabelStepGreen(int stepNum);
    void setLabelArrowGrey(int arrowNum);
    void setLabelArrowGreen(int arrowNum);
    CommSignal * guiToSysBridgeSig;
    void commSigProcessInit(class commWithBridge * commRos);
    void printLayoutInfo();

protected:
    void keyPressEvent(QKeyEvent * event) override;

public slots:
    void processSysBridgeSig(int sysSig);

private slots:
    void on_pushButton_start_clicked();
    void on_pushButton_stop_clicked();
    void on_pushButton_pause_clicked();
    void on_pushButton_auto_clicked();

    void on_pushButton_ch_F_pressed();
    void on_pushButton_ch_F_released();
    void on_pushButton_ch_B_pressed();
    void on_pushButton_ch_B_released();
    void on_pushButton_ch_L_pressed();
    void on_pushButton_ch_L_released();
    void on_pushButton_ch_R_pressed();
    void on_pushButton_ch_R_released();
    
    void on_pushButton_grip_L_toggled(bool checked);
    void on_pushButton_grip_R_toggled(bool checked);
    void robotEn_pressed(bool checked);
    void left_arm_selected(bool checked);
    void right_arm_selected(bool checked);
    void pushButton_X_plus_clicked();
    void pushButton_X_minus_clicked();
    void pushButton_Y_plus_clicked();
    void pushButton_Y_minus_clicked();
    void pushButton_Z_plus_clicked();
    void pushButton_Z_minus_clicked();
    void pushButton_Rx_plus_clicked();
    void pushButton_Rx_minus_clicked();
    void pushButton_Ry_plus_clicked();
    void pushButton_Ry_minus_clicked();
    void pushButton_Rz_plus_clicked();
    void pushButton_Rz_minus_clicked();
    void onLongPressTimeout();

private:
    Ui::MainWindow *ui;
    void widgetsInit();
    void autoModeStatusSet();
    void autoModeCtrlBtnsSet();
    void autoModeTaskSet();
    void autoModeAnimationSet();
    void autoModePageHide();
    void autoModePageShow();
    void autoModeLayoutSet();

    void manualModePageHide();
    void manualModePageShow();
    void manualModePosSet();
    void manualGripperSet();
    void manualChasisSet();
    void manualArmSet();
    void manualArmJointsInfoSet();
    void manualArmPosInfoSet();
    void manualConnectSlots();
    void manualModeLayoutSet();

    void autoSwitchToManual();
    void manualSwitchToAuto();

    QGridLayout *mainLayout;
    QGridLayout *autoModeLayout;
    QWidget *autoModeWidget;
    QGridLayout *manualModeLayout;
    QWidget *manualModeWidget;
    QGridLayout *modeStatusLayout;
    QWidget *modeStatusWidget;

    QHBoxLayout *ctrlBtnsLayout;
    QWidget *ctrlBtnsWidget;

    QVBoxLayout *taskStatusLayout;
    QWidget *taskStatusWidget;

    QGridLayout *posDisplayLayout;
    QWidget *posDisplayWidget;
    QHBoxLayout *pos_x_layout;
    QWidget *pos_x_widget;
    QHBoxLayout *pos_y_layout;
    QWidget *pos_y_widget;

    QGridLayout *chCtrlLayout;
    QWidget *chCtrlWidget;

    QGridLayout *armCtrlLayout;
    QWidget *armCtrlWidget;

    QGraphicsView *flowchartView;
    QGraphicsScene *flowchartScene;

    AnimationWidget *animationWidget;

    class commWithBridge *commBridge;

    bool isAutoMode = true;

    QPushButton * robotEnPushbutton;

    QLabel *label_joint;
    QLabel *label_joint_L;
    QLabel *label_joint_R;
    QLabel *label_L_arm_J1;
    QLabel *label_L_arm_J2;
    QLabel *label_L_arm_J3;
    QLabel *label_L_arm_J4;
    QLabel *label_L_arm_J5;
    QLabel *label_L_arm_J6;
    QLabel *label_L_arm_J7;

    QLineEdit *lineEdit_L_arm_J1;
    QLineEdit *lineEdit_L_arm_J2;
    QLineEdit *lineEdit_L_arm_J3;
    QLineEdit *lineEdit_L_arm_J4;
    QLineEdit *lineEdit_L_arm_J5;
    QLineEdit *lineEdit_L_arm_J6;
    QLineEdit *lineEdit_L_arm_J7;

    QHBoxLayout *L_arm_J1_layout;
    QHBoxLayout *L_arm_J2_layout;
    QHBoxLayout *L_arm_J3_layout;
    QHBoxLayout *L_arm_J4_layout;
    QHBoxLayout *L_arm_J5_layout;
    QHBoxLayout *L_arm_J6_layout;
    QHBoxLayout *L_arm_J7_layout;
    QWidget *L_arm_J1_widget;
    QWidget *L_arm_J2_widget;
    QWidget *L_arm_J3_widget;
    QWidget *L_arm_J4_widget;
    QWidget *L_arm_J5_widget;
    QWidget *L_arm_J6_widget;
    QWidget *L_arm_J7_widget;

    QLabel *label_R_arm_J1;
    QLabel *label_R_arm_J2;
    QLabel *label_R_arm_J3;
    QLabel *label_R_arm_J4;
    QLabel *label_R_arm_J5;
    QLabel *label_R_arm_J6;
    QLabel *label_R_arm_J7;

    QLineEdit *lineEdit_R_arm_J1;
    QLineEdit *lineEdit_R_arm_J2;
    QLineEdit *lineEdit_R_arm_J3;
    QLineEdit *lineEdit_R_arm_J4;
    QLineEdit *lineEdit_R_arm_J5;
    QLineEdit *lineEdit_R_arm_J6;
    QLineEdit *lineEdit_R_arm_J7;

    QHBoxLayout *R_arm_J1_layout;
    QHBoxLayout *R_arm_J2_layout;
    QHBoxLayout *R_arm_J3_layout;
    QHBoxLayout *R_arm_J4_layout;
    QHBoxLayout *R_arm_J5_layout;
    QHBoxLayout *R_arm_J6_layout;
    QHBoxLayout *R_arm_J7_layout;
    QWidget *R_arm_J1_widget;
    QWidget *R_arm_J2_widget;
    QWidget *R_arm_J3_widget;
    QWidget *R_arm_J4_widget;
    QWidget *R_arm_J5_widget;
    QWidget *R_arm_J6_widget;
    QWidget *R_arm_J7_widget;

    QGridLayout *armJointLayout;
    QWidget *armJointWidget;

    QLabel *label_coord;
    QLabel *label_coord_L;
    QLabel *label_coord_R;
    QLabel *label_L_arm_X;
    QLabel *label_L_arm_Y;
    QLabel *label_L_arm_Z;
    QLabel *label_L_arm_Rx;
    QLabel *label_L_arm_Ry;
    QLabel *label_L_arm_Rz;

    QLineEdit *lineEdit_L_arm_X;
    QLineEdit *lineEdit_L_arm_Y;
    QLineEdit *lineEdit_L_arm_Z;
    QLineEdit *lineEdit_L_arm_Rx;
    QLineEdit *lineEdit_L_arm_Ry;
    QLineEdit *lineEdit_L_arm_Rz;

    QHBoxLayout *L_arm_X_layout;
    QHBoxLayout *L_arm_Y_layout;
    QHBoxLayout *L_arm_Z_layout;
    QHBoxLayout *L_arm_Rx_layout;
    QHBoxLayout *L_arm_Ry_layout;
    QHBoxLayout *L_arm_Rz_layout;
    QWidget *L_arm_X_widget;
    QWidget *L_arm_Y_widget;
    QWidget *L_arm_Z_widget;
    QWidget *L_arm_Rx_widget;
    QWidget *L_arm_Ry_widget;
    QWidget *L_arm_Rz_widget;

    QHBoxLayout *R_arm_X_layout;
    QHBoxLayout *R_arm_Y_layout;
    QHBoxLayout *R_arm_Z_layout;
    QHBoxLayout *R_arm_Rx_layout;
    QHBoxLayout *R_arm_Ry_layout;
    QHBoxLayout *R_arm_Rz_layout;
    QWidget *R_arm_X_widget;
    QWidget *R_arm_Y_widget;
    QWidget *R_arm_Z_widget;
    QWidget *R_arm_Rx_widget;
    QWidget *R_arm_Ry_widget;
    QWidget *R_arm_Rz_widget;

    QLabel *label_R_arm_X;
    QLabel *label_R_arm_Y;
    QLabel *label_R_arm_Z;
    QLabel *label_R_arm_Rx;
    QLabel *label_R_arm_Ry;
    QLabel *label_R_arm_Rz;

    QLineEdit *lineEdit_R_arm_X;
    QLineEdit *lineEdit_R_arm_Y;
    QLineEdit *lineEdit_R_arm_Z;
    QLineEdit *lineEdit_R_arm_Rx;
    QLineEdit *lineEdit_R_arm_Ry;
    QLineEdit *lineEdit_R_arm_Rz;

    QHBoxLayout *armHBoxLayout;
    QWidget *armHBoxWidget;
    QGridLayout *armCoordLayout;
    QWidget *armCoordWidget;

    bool isLeftArmSelected = true;
    QRadioButton *leftArmRadioButton;
    QRadioButton *rightArmRadioButton;
    QButtonGroup *armSelectionGroup;

    QPushButton *pushButton_X_plus;
    QPushButton *pushButton_X_minus;
    QWidget *pushButton_X_container;

    QPushButton *pushButton_Y_plus;
    QPushButton *pushButton_Y_minus;
    QWidget *pushButton_Y_container;

    QPushButton *pushButton_Z_plus;
    QPushButton *pushButton_Z_minus;
    QWidget *pushButton_Z_container;

    QPushButton *pushButton_Rx_plus;
    QPushButton *pushButton_Rx_minus;
    QWidget *pushButton_Rx_container;

    QPushButton *pushButton_Ry_plus;
    QPushButton *pushButton_Ry_minus;
    QWidget *pushButton_Ry_container;

    QPushButton *pushButton_Rz_plus;
    QPushButton *pushButton_Rz_minus;
    QWidget *pushButton_Rz_container;

    QTimer *longPressTimer;
};

#endif // MAINWINDOW_H
